
import bpy


from 全局控制.Node.节点和插座基类.bl_UpdateNode import *
from 对象操作 import bl_对象变换帧

import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类
from 全局控制.Node.节点数据类型 import bl_集合数据类型

from bpy.types import Operator, Menu
from bl_operators.presets import AddPresetBase




class C_选择修改器项(Operator):
	bl_idname = "node.object_modifiers_set_selete"
	bl_label = "Add Object Draw Preset"

	m_修改器类型 = bpy.props.StringProperty(default='')
	def execute(self, context):
		#node_group = bpy.data.node_groups[self.m_节点树ID名称]
		#node = node_group.nodes[self.m_节点ID名称]
		print(self, self.name, self.m_修改器类型)
		return {'FINISHED'}


class C_修改器选择菜单(Menu):
	bl_label = "修改器"
	#preset_subdir = "object/draw"
	#draw = Menu.draw_preset

	def draw(self, context):
		layout = self.layout
		列布局 = layout.row()
		行布局 = 列布局.column()
		行布局.label(text='修改')
		行布局.operator("node.object_modifiers_set_selete", text='UV Project').m_修改器类型 = 'UV Project'
		行布局.operator("node.object_modifiers_set_selete", text='UV Project').m_修改器类型 = 'mbt'

def f_投影物体数量update(self, context):
	差数 = len(self.m_投影物体) - self.m_projector_count
	print('差数 = ', 差数)
	if 差数 > 0 :
		for i in range(差数):
			self.m_投影物体.pop(-1)
	else:
		差数 = -差数
		for i in range(差数):
			self.m_投影物体.add()

class C_SN_物体修改器(基类.C_SN_基类):
	bl_dname = 'bl_node_物体修改器'
	bl_label = '物体修改器'
	
	m_修改器类型 = bpy.props.StringProperty(name="修改器类型")
	m_投影物体 = bpy.props.CollectionProperty(type=bl_集合数据类型.C_物体组元素)
	m_projector_count = bpy.props.IntProperty(update=f_投影物体数量update)
	def init(self, context):
		super(C_SN_物体修改器, self).f_init(context)
		
		self.inputs.new('C_NodeSocket_物体', name='修改对象')
		self.inputs.new('C_NodeSocket_物体', name='投影物体')
		#self.inputs.new('C_NodeSocket_bool', name='是否隐藏')

	def draw_buttons(self, context, layout):
		layout.menu(C_修改器选择菜单.__name__, text=C_修改器选择菜单.bl_label)
		#layout.operator_menu_enum("object.modifier_add", "type")
		

		split = layout.split()
		col = split.column()
		#col.prop_search(md, "uv_layer", ob.data, "uv_layers")
		col.separator()

		if self.inputs['投影物体'].is_linked == False :
			col.prop(self, "m_projector_count", text="Projectors")
			for proj in self.m_投影物体:
				col.prop(proj, "value", text="")
				


class C_NODE_MENU_物体修改器(bpy.types.Operator):
	bl_idname = 'node.global_node_obj_modifiers'
	bl_label = '物体修改器'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_SN_物体修改器', use_transform=True)
		return {'FINISHED'}





classes = (
	C_选择修改器项,
	C_修改器选择菜单,

	C_SN_物体修改器,
	C_NODE_MENU_物体修改器,
	
)


def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)




